﻿trimString = function (str)
{
	var i,j;

	if(str == "") return "";

	for(i=0;i<str.length;i++)
	if(str.charAt(i) != ' ') break;
	if(i >= str.length) return "";

	for(j=str.length-1;j>=0;j--)
	if(str.charAt(j) != ' ') break;

	return str.substring(i,j+1);
}

bytes = function (str){
    if(typeof(str)!='string'){
        str = str.value;
    }
    var len = 0;
    for(var i = 0; i < str.length; i++){
        if(str.charCodeAt(i) > 127){
            len++;
        }
        len++;
    }
    return len;
}

//no use yet, use with addError
clearErrorMessages = function (form)
{
	return;
}

//no use yet, use with addError
clearErrorLabels = function (form)
{
	return;
}

//public functions
processError = function (field, errMsg, showId, select)
{
    if (null == showId)
    {
	    alert(errMsg);
    }
    else
    {
        var o = document.getElementById(showId);
        o.style.display = 'block';
        o.innerHTML = '<p class="wrong"><img src="/images/nosuccess.gif">&nbsp;&nbsp;'+errMsg+'</p>';
    }
    if (null == select)
    {
	    field.select();
    }
	return false;
}
processSucc = function (field, errMsg, showId, select)
{
    if (null == showId)
    {
	    alert(errMsg);
    }
    else
    {
        var o = document.getElementById(showId);
//        o.style.display = 'block';
        
        //o.innerHTML = '<p class="right">'+errMsg+'</p>';
		o.innerHTML = '<p class="right"><img src="/images/success.gif"></p>';
    }
	/**
    if (null == select)
    {
	    field.select();
    }
	*/
	return true;
}

hideErr = function(id)
{
    document.getElementById(id).style.display = 'none';
    return true;
}

//private functions
//to add the errors for the form-validate
addError = function (e, errorText) 
{
	try
	{
		var o = getErrorShowDiv();
		o.innerHTML = '错误提示: ' + errorText;
		e.focus();
	}
	catch (e)
	{
		alert(e);
	}
	return false;
}

Checker = function()
{
	this.checkRequired=function(fieldValue)
	{
		fieldValue = trimString(fieldValue);
		return ('' != fieldValue);
	}
	
	this.checkRequiredstring=function(fieldValue)
	{
		fieldValue = trimString(fieldValue);
		return ( ('' != fieldValue) && (0 < fieldValue.length) );
	}
	
	this.checkEmail=function(fieldValue, onlyIfAvailable)
	{
		fieldValue = trimString(fieldValue);
		if (onlyIfAvailable && ('' == fieldValue))
		{
			return true;
		}
		return ( this.checkRequiredstring(fieldValue) && (this.checkRegex(fieldValue, /^\S+@\S+\.(com|net|org|info|edu|mil|gov|biz|ws|us|tv|cc|aero|arpa|coop|int|jobs|museum|name|pro|travel|nato|.{2,2})$/gi)) );
	}
	
	this.checkUrl=function(fieldValue, onlyIfAvailable)
	{
		fieldValue = trimString(fieldValue);
		if (onlyIfAvailable && ('' == fieldValue))
		{
			return true;
		}
		return ( this.checkRequiredstring(fieldValue) && (this.checkRegex(fieldValue, /^((file:\/\/\S+)|(ftp|http|https):\/\/\S+\.(com|net|org|info|edu|mil|gov|biz|ws|us|tv|cc|aero|arpa|coop|int|jobs|museum|name|pro|travel|nato|.{2,2}))$/gi)) );
	}
	
	this.checkInt=function(fieldValue, min, max, onlyIfAvailable)
	{
		fieldValue = trimString(fieldValue);
		if (onlyIfAvailable && ('' == fieldValue))
		{
			return true;
		}
		return ( this.checkRequiredstring(fieldValue) && (parseInt(fieldValue) >= min && parseInt(fieldValue) <= max) );
	}
	
	this.checkStringlength=function(fieldValue, min, max, onlyIfAvailable)
	{
		fieldValue = trimString(fieldValue);
		if (onlyIfAvailable && ('' == fieldValue))
		{
			return true;
		}
		return (this.checkRequiredstring(fieldValue) && (fieldValue.length >= min && fieldValue.length <= max) );
	}
	
	this.checkRegex = function (fieldValue, reg)
	{
		fieldValue = trimString(fieldValue);
		return (null != fieldValue.match(reg));
	}
	
	this.checkDouble = function (fieldValue, minInclusive, maxInclusive, minExclusive, maxExclusive)
	{
		fieldValue = trimString(fieldValue);
		return ( (false == minInclusive) && (false == maxInclusive) && (false == minExclusive) && (false == maxExclusive) );
	}
	
	this.checkPhone = function(fieldValue, onlyIfAvailable)
	{
		fieldValue = trimString(fieldValue);
		if (onlyIfAvailable && ('' == fieldValue))
		{
			return true;
		}
		return (this.checkRequiredstring(fieldValue) && this.checkRegex(fieldValue, '^[0-9]{3,5}-[0-9\(\)\-\s]{6,20}$'));
	}
	
    /*
    this.checkUserName = function(fieldValue, onlyIfAvailable)
    {
        fieldValue = trimString(fieldValue);
        if (onlyIfAvailable && ('' == fieldValue))
        {
            return true;
        }
        return ( this.checkRegex(fieldValue, /^[a-z]{1}[a-z0-9_.]*[0-9a-z]{1}$/) && this.checkStringlength(fieldValue, 5, 30) );
    }*/

    
	this.checkUserName = function(fieldValue, onlyIfAvailable, err)
    {
	    return this.checkNickName(fieldValue, onlyIfAvailable, err);
    }

    this.checkUserNameForOld = function(fieldValue, onlyIfAvailable, err)
	{
		fieldValue = trimString(fieldValue);
		if (onlyIfAvailable && ('' == fieldValue))
		{
			return true;
		}
        if (false == this.checkRequiredstring(fieldValue))
        {
            return false;
        }
        if (bytes(fieldValue) > 30)
        {
            return false;
        }
        var ch;
        //46=. 95=_
        for(var i = 0; i < fieldValue.length; i++)
        {
            ch = fieldValue.charCodeAt(i);
            if ( (46 != ch) && (95 != ch) && ((ch < 127) && !fieldValue.substr(i,1).match(/^\w+$/ig)) )
            {
                err.msg = (String.fromCharCode(ch));
                return false;
            }
        }
        return true;
	}

	this.checkUserNameLength = function(fieldValue, onlyIfAvailable)
	{
		fieldValue = trimString(fieldValue);
		if (onlyIfAvailable && ('' == fieldValue))
		{
			return true;
		}
        if (false == this.checkRequiredstring(fieldValue))
        {
            return false;
        }
        if ( bytes(fieldValue) <2 && bytes(fieldValue) > 14)
        {
            return false;
        }
        return true;
    }

	this.checkNickName = function(fieldValue, onlyIfAvailable, err)
	{
        if (false == this.checkUserNameLength(fieldValue, onlyIfAvailable))
        {
            return false;
        }
        var ch;
        //46=. 95=_
        for(var i = 0; i < fieldValue.length; i++)
        {
            ch = fieldValue.charCodeAt(i);
            if ( (46 != ch) && (95 != ch) && ((ch < 127) && !fieldValue.substr(i,1).match(/^\w+$/ig)) )
            {
                err.msg = (String.fromCharCode(ch));
                return false;
            }
        }
        return true;
	}

	this.checkPassword = function(fieldValue, onlyIfAvailable)
	{
		fieldValue = trimString(fieldValue);
		if (onlyIfAvailable && ('' == fieldValue))
		{
			return true;
		}
		return this.checkStringlength(fieldValue, 6, 20);
	}
	
	this.checkPostcode = function(fieldValue, onlyIfAvailable)
	{
		fieldValue = trimString(fieldValue);
		if (onlyIfAvailable && ('' == fieldValue))
		{
			return true;
		}
		return (this.checkRequiredstring(fieldValue) && this.checkRegex(fieldValue, '^[0-9]{6}$'));
	}

    this.checkUserImg = function(fieldValue, onlyIfAvailable)
	{
		fieldValue = trimString(fieldValue);
		if (onlyIfAvailable && ('' == fieldValue))
		{
			return true;
		}

        var arr = fieldValue.split('.');
        var ext = arr[arr.length-1].toLowerCase();
        var exts = new Array('jpg', 'jpeg', 'gif', 'png');
        for (var i=0;i<exts.length;i++)
        {
            if (ext == exts[i])
            {
                return true;
            }
        }
        return false;
	}

}
